'''
Created on May 14, 2010

@author: Lukasz Spintzyk
'''
import unittest
import os, time
from fsutils.utils import mount_filesystem, create_filesystem,\
    umount_filesystem
    
import fsutils.utils

class FileReadWrite(unittest.TestCase):


    def setUp(self):
        key = r'AA::22::33::44::55::66::77::88::01::22::33::44::55::66::77::88'
        block_size = 1024
        blocks_per_group = 245
        inodes_per_group = 240
        groups_count = 1000
        
        
        device = os.path.join(os.getcwd() ,"../../tst_py.fs")
        self.mount_point = os.path.join(os.getcwd(), "../../mount_point")
        if not os.path.exists(self.mount_point):
            os.mkdir(self.mount_point)
        
        open(device, "w").close()
        
        create_filesystem(device, key, groups_count, block_size, blocks_per_group, inodes_per_group)
        
        mount_filesystem(self.mount_point, key, device)
        time.sleep(0.1)
        self.dir = os.getcwd()
        
        os.chdir(self.mount_point)
        
        self.filename = "plik"
        self.fw = open(self.filename, 'w')
#TODO touch plik;  touch plik

    def tearDown(self):
        os.chdir(self.dir)
        time.sleep(0.1)
        umount_filesystem(self.mount_point)
        time.sleep(0.1)
        os.chdir(self.mount_point)
        os.system("rm -rf *")
        os.chdir(self.dir)
        pass

    def testWriteLessThanBlock(self):
        print "testWriteLessThanBlock"
        s = fsutils.utils.getRandText(1020)
        assert (len(s) == 1020)
        
        self.fw.write(s)
        self.fw.close()
        stats = os.stat(self.filename)
        assert(stats.st_size == len(s))
        
        self.fw = open(self.filename, 'r')
        content = r''.join(self.fw.readlines())
        assert(len(content) == len(s))
        assert(s == content)
        self.fw.close()

         
    def testWriteBlock(self):
        print "testWriteBlock"
        s = fsutils.utils.getRandText(1024)
        assert (len(s) == 1024)
        
        self.fw.write(s)
        self.fw.close()
        stats = os.stat(self.filename)
        assert(stats.st_size == len(s))
        
        self.fw = open(self.filename, 'r')
        content = r''.join(self.fw.readlines())
        assert(len(content) == len(s))
        assert(s == content)
        print "lol"
        self.fw.close()

    def testWriteBlockAndHalf(self):
        print "testWriteBlockAndHalf"
        s = fsutils.utils.getRandText(1024+120)
        assert (len(s) == 1024+120)
        
        self.fw.write(s)
        self.fw.close()
        stats = os.stat(self.filename)
        assert(stats.st_size == len(s))
        
        self.fw = open(self.filename, 'r')
        content = r''.join(self.fw.readlines())
        assert(len(content) == len(s))
        assert(s == content)
        self.fw.close()

    def testWrite2Blocks(self):
        print "testWrite2Blocks"
        s = fsutils.utils.getRandText(2*1024)
        assert (len(s) == 1024*2)
        
        self.fw.write(s)
        self.fw.close()
        stats = os.stat(self.filename)
        assert(stats.st_size == len(s))
        
        self.fw = open(self.filename, 'r')
        content = r''.join(self.fw.readlines())
        assert(len(content) == len(s))
        assert(s == content)
        self.fw.close()
        
    def testWrite20Blocks(self):
        print "testWrite20Blocks"
        s = fsutils.utils.getRandText(1024*20+231)
        assert (len(s) == 1024*20+231)
        self.fw.write(s)
        self.fw.close()
        stats = os.stat(self.filename)
        assert(stats.st_size == len(s))
        
        self.fw = open(self.filename, 'r')
        content = r''.join(self.fw.readlines())
        assert(len(content) == len(s))
        assert(s == content)
        self.fw.close()
        
    def testCreateRenemeCreateFile(self):
        print "testCreateRenemeCreateFile"
        s = fsutils.utils.getRandText(123)
        assert (len(s) == 123)
        
        name = "kod.cpp"
        backup_name = "kod.cpp~"
        fp_kod = open("kod.cpp", "w")
        fp_kod.write(s)
        os.rename("kod.cpp", "kod.cpp~")
        
        fp_kod2 = open("kod.cpp", "w")
        fp_kod2.write(s)
        fp_kod.close()
        os.remove("kod.cpp~")
        
        fp_kod2.close()
        
        fp_kod_read = open("kod.cpp")
        s2 = ''.join(fp_kod_read.readlines())
        fp_kod_read.close()
        assert(s2 == s);
        